package com.ykunn.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ykunn.entity.UserEntity;
import com.ykunn.mapper.UserEntityMapper;
import com.ykunn.utils.json.ResultCode;
import com.ykunn.utils.json.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserEntityMapper userEntityMapper;

    @RequestMapping("/api")
    public PageInfo<UserEntity> getAllOfPage(Integer page){
        PageInfo<UserEntity> pageInfo = null;
        try {
            PageHelper.startPage(page,5);
            List<UserEntity>  userEntities = userEntityMapper.selectAllOfpage();
            pageInfo = new PageInfo<>(userEntities);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            /*前端需要判空*/
            return null;
        }
    }

    @RequestMapping(value = "api/user",method = RequestMethod.POST)
    public ResultJson getUserById(Integer id){
        try {
           return new ResultJson(userEntityMapper.selectByPrimaryKey(id));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultJson(ResultCode.EXCEPTION,e.getMessage());
        }
    }


    @RequestMapping(value = "api/user/page",method = RequestMethod.POST)
    public ResultJson getAllOfPage2(Integer page){
        try {
            PageHelper.startPage(page,5);
            List<UserEntity> userEntities = userEntityMapper.selectAllOfpage();
            return new ResultJson(new PageInfo<>(userEntities));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultJson(ResultCode.EXCEPTION,e.getMessage());
        }
    }
}
